home *** CD-ROM | disk | FTP | other *** search
- /*----------------------------------------------------------------------------
-
- drawutil.c
-
- This reusable module contains miscellaneous drawing utility routines.
-
- Copyright © 1994-1995, Northwestern University.
-
- ----------------------------------------------------------------------------*/
-
- #include <Palettes.h>
-
- #include "def.h"
- #include "drawutil.h"
-
-
-
- /*----------------------------------------------------------------------------
- HasColorQD
-
- Check to see if color QuickDraw is available.
-
- Exit: function result = true if we have color QD.
- ----------------------------------------------------------------------------*/
-
- Boolean HasColorQD (void)
- {
- OSErr err = noErr;
- long quickdrawVersion;
- static Boolean haveChecked = false;
- static Boolean haveColorQD;
-
- if (!haveChecked) {
- err = Gestalt(gestaltQuickdrawVersion, &quickdrawVersion);
- haveColorQD = err == noErr && quickdrawVersion >= gestalt8BitQD;
- }
- return haveColorQD;
- }
-
-
-
- /*----------------------------------------------------------------------------
- SetPortTextStyle
-
- Set the font, size, and style of the current port.
-
- Entry: *style = text style record.
- ----------------------------------------------------------------------------*/
-
- void SetPortTextStyle (TextStyle *style)
- {
- TextFont(style->tsFont);
- TextFace(style->tsFace);
- TextSize(style->tsSize);
- }
-
-
-
- /*----------------------------------------------------------------------------
- GetPortTextStyle
-
- Get the font, size, and style of the current port.
-
- Exit: *style = text style record.
- ----------------------------------------------------------------------------*/
-
- void GetPortTextStyle (TextStyle *style)
- {
- style->tsFont = qd.thePort->txFont;
- style->tsFace = qd.thePort->txFace;
- style->tsSize = qd.thePort->txSize;
- }
-
-
-
- /*----------------------------------------------------------------------------
- DrawGrayRect
-
- Draw a gray rectangle.
-
- Entry: r = pointer to rectangle.
- ----------------------------------------------------------------------------*/
-
- static pascal void DrawGrayRectOneDepth (short depth, short deviceFlags,
- GDHandle targetDevice, long userData)
- {
- RGBColor fg, bg, g;
-
- if (targetDevice != nil) {
- GetForeColor(&fg);
- GetBackColor(&bg);
- g = fg;
- if (GetGray(targetDevice, &bg, &g)) {
- RGBForeColor(&g);
- FrameRect((Rect*)userData);
- RGBForeColor(&fg);
- return;
- }
- }
- PenPat(&qd.gray);
- FrameRect((Rect*)userData);
- PenPat(&qd.black);
- }
-
- void DrawGrayRect (Rect *r)
- {
- static DeviceLoopDrawingUPP upp = nil;
-
- if (upp == nil) upp = NewDeviceLoopDrawingProc(DrawGrayRectOneDepth);
- DeviceLoop(qd.thePort->visRgn, upp, (long)r, 0);
- }
-
-
-
- /*----------------------------------------------------------------------------
- DrawGrayRoundRect
-
- Draw a gray round rectangle.
-
- Entry: r = pointer to rectangle.
- ovalWidth = oval width.
- ovalHeight = oval height.
- ----------------------------------------------------------------------------*/
-
- typedef struct TDrawGrayRoundRectUserData {
- Rect *r;
- short ovalWidth;
- short ovalHeight;
- } TDrawGrayRoundRectUserData;
-
- static pascal void DrawGrayRoundRectOneDepth (short depth, short deviceFlags,
- GDHandle targetDevice, long userData)
- {
- RGBColor fg, bg, g;
- TDrawGrayRoundRectUserData *ud;
-
- ud = (TDrawGrayRoundRectUserData*)userData;
- if (targetDevice != nil) {
- GetForeColor(&fg);
- GetBackColor(&bg);
- g = fg;
- if (GetGray(targetDevice, &bg, &g)) {
- RGBForeColor(&g);
- FrameRoundRect(ud->r, ud->ovalWidth, ud->ovalHeight);
- RGBForeColor(&fg);
- return;
- }
- }
- PenPat(&qd.gray);
- FrameRoundRect(ud->r, ud->ovalWidth, ud->ovalHeight);
- PenPat(&qd.black);
- }
-
- void DrawGrayRoundRect (Rect *r, short ovalWidth, short ovalHeight)
- {
- static DeviceLoopDrawingUPP upp = nil;
- TDrawGrayRoundRectUserData ud;
-
- ud.r = r;
- ud.ovalWidth = ovalWidth;
- ud.ovalHeight = ovalHeight;
- if (upp == nil) upp = NewDeviceLoopDrawingProc(DrawGrayRoundRectOneDepth);
- DeviceLoop(qd.thePort->visRgn, upp, (long)&ud, 0);
- }
-
-
-
- /*----------------------------------------------------------------------------
- DrawColorPict
-
- Draw a color picture.
-
- Entry: colorID = resource id of color version of PICT.
- bwID = resource id of black and white version of PICT.
- r = pointer to rectangle.
- ----------------------------------------------------------------------------*/
-
- typedef struct TDrawColorPictUserData {
- short colorID;
- short bwID;
- Rect *r;
- } TDrawColorPictUserData;
-
- static pascal void DrawColorPictOneDepth (short depth, short deviceFlags,
- GDHandle targetDevice, long userData)
- {
- TDrawColorPictUserData *ud;
-
- ud = (TDrawColorPictUserData*)userData;
- if (targetDevice != nil && depth > 2) {
- DrawPicture(GetPicture(ud->colorID), ud->r);
- } else {
- DrawPicture(GetPicture(ud->bwID), ud->r);
- }
- }
-
- void DrawColorPict (short colorID, short bwID, Rect *r)
- {
- TDrawColorPictUserData ud;
- static DeviceLoopDrawingUPP upp = nil;
-
- ud.colorID = colorID;
- ud.bwID = bwID;
- ud.r = r;
- if (upp == nil) upp = NewDeviceLoopDrawingProc(DrawColorPictOneDepth);
- DeviceLoop(qd.thePort->visRgn, upp, (long)&ud, 0);
- }
-
-
-
- /*----------------------------------------------------------------------------
- FillColorPoly
-
- Fill a polygon in color.
-
- Entry: poly = handle to polygon.
- color = pointer to color.
- pat = pattern.
- ----------------------------------------------------------------------------*/
-
- typedef struct TFillColorPolyUserData {
- PolyHandle poly;
- RGBColor *color;
- ConstPatternParam pat;
- } TFillColorPolyUserData;
-
- static pascal void FillColorPolyOneDepth (short depth, short deviceFlags,
- GDHandle targetDevice, long userData)
- {
- TFillColorPolyUserData *ud;
- RGBColor fgColor;
-
- ud = (TFillColorPolyUserData*)userData;
- if (targetDevice != nil && depth > 2) {
- GetForeColor(&fgColor);
- RGBForeColor(ud->color);
- FillPoly(ud->poly, ud->pat);
- RGBForeColor(&fgColor);
- }
- }
-
- void FillColorPoly (PolyHandle poly, RGBColor *color, ConstPatternParam pat)
- {
- TFillColorPolyUserData ud;
- static DeviceLoopDrawingUPP upp = nil;
-
- ud.poly = poly;
- ud.color = color;
- ud.pat = pat;
- if (upp == nil) upp = NewDeviceLoopDrawingProc(FillColorPolyOneDepth);
- DeviceLoop(qd.thePort->visRgn, upp, (long)&ud, 0);
- }
-
-
-
- /*----------------------------------------------------------------------------
- FrameGrayPoly
-
- Frame a polygon in gray.
-
- Entry: poly = handle to polygon.
-
- On B&W monitors, framing in gray doesn't work very well, so in that case
- we frame in black.
- ----------------------------------------------------------------------------*/
-
- static pascal void FrameGrayPolyOneDepth (short depth, short deviceFlags,
- GDHandle targetDevice, long userData)
- {
- RGBColor fg, bg, g;
-
- if (targetDevice != nil) {
- GetForeColor(&fg);
- GetBackColor(&bg);
- g = fg;
- if (GetGray(targetDevice, &bg, &g)) {
- RGBForeColor(&g);
- FramePoly((PolyHandle)userData);
- RGBForeColor(&fg);
- return;
- }
- }
- FramePoly((PolyHandle)userData);
- }
-
- void FrameGrayPoly (PolyHandle poly)
- {
- static DeviceLoopDrawingUPP upp = nil;
-
- if (upp == nil) upp = NewDeviceLoopDrawingProc(FrameGrayPolyOneDepth);
- DeviceLoop(qd.thePort->visRgn, upp, (long)poly, 0);
- }
-
-
-
- /*----------------------------------------------------------------------------
- FillColorRect
-
- Fill a rectangle in color.
-
- Entry: r = pointer to rectangle.
- color = pointer to color to use if depth > 2.
- pat = B&W pattern to use if depth <= 2.
- ----------------------------------------------------------------------------*/
-
- typedef struct TFillColorRectUserData {
- Rect *r;
- RGBColor *color;
- ConstPatternParam pat;
- } TFillColorRectUserData;
-
- static pascal void FillColorRectOneDepth (short depth, short deviceFlags,
- GDHandle targetDevice, long userData)
- {
- TFillColorRectUserData *ud;
- RGBColor fgColor;
-
- ud = (TFillColorRectUserData*)userData;
- if (targetDevice != nil && depth > 2) {
- GetForeColor(&fgColor);
- RGBForeColor(ud->color);
- PaintRect(ud->r);
- RGBForeColor(&fgColor);
- } else {
- FillRect(ud->r, ud->pat);
- }
- }
-
- void FillColorRect (Rect *r, RGBColor *color, ConstPatternParam pat)
- {
- TFillColorRectUserData ud;
- static DeviceLoopDrawingUPP upp = nil;
-
- if (upp == nil)
- upp = NewDeviceLoopDrawingProc(FillColorRectOneDepth);
- ud.r = r;
- ud.color = color;
- ud.pat = pat;
- DeviceLoop(qd.thePort->visRgn, upp, (long)&ud, 0);
- }
-
-
-
- /*----------------------------------------------------------------------------
- FillGrayRect
-
- Fill a rectangle with gray.
-
- Entry: r = pointer to rectangle.
- ----------------------------------------------------------------------------*/
-
- static pascal void FillGrayRectOneDepth (short depth, short deviceFlags,
- GDHandle targetDevice, long userData)
- {
- Rect *r;
- RGBColor fg, bg, g;
-
- r = (Rect*)userData;
- if (targetDevice != nil) {
- GetForeColor(&fg);
- GetBackColor(&bg);
- g = fg;
- if (GetGray(targetDevice, &bg, &g)) {
- RGBForeColor(&g);
- PaintRect(r);
- RGBForeColor(&fg);
- return;
- }
- }
- FillRect(r, &qd.gray);
- }
-
- void FillGrayRect (Rect *r)
- {
- static DeviceLoopDrawingUPP upp = nil;
-
- if (upp == nil)
- upp = NewDeviceLoopDrawingProc(FillGrayRectOneDepth);
- DeviceLoop(qd.thePort->visRgn, upp, (long)r, 0);
- }
-
-
-
- /*----------------------------------------------------------------------------
- ComplementRgn
-
- Complement a QuickDraw region.
-
- Entry: rgn = handle to region.
-
- Exit: Region complemented.
- ----------------------------------------------------------------------------*/
-
- void ComplementRgn (RgnHandle rgn)
- {
- static RgnHandle bigRgn = nil;
-
- if (bigRgn == nil) {
- bigRgn = NewRgn();
- SetRectRgn(bigRgn, -0x7fff, -0x7fff, 0x7fff, 0x7fff);
- }
- DiffRgn(bigRgn, rgn, rgn);
- }
-
-
-
- /*----------------------------------------------------------------------------
- LocalToGlobalRect
-
- Convert a rectangle from local to global coordinates.
-
- Entry: r = pointer to rectangle.
- ----------------------------------------------------------------------------*/
-
- void LocalToGlobalRect (Rect *r)
- {
- LocalToGlobal((Point*)&r->top);
- LocalToGlobal((Point*)&r->bottom);
- }
-
-
-
- /*----------------------------------------------------------------------------
- GlobalToLocalRect
-
- Convert a rectangle from global to local coordinates.
-
- Entry: r = pointer to rectangle.
- ----------------------------------------------------------------------------*/
-
- void GlobalToLocalRect (Rect *r)
- {
- GlobalToLocal((Point*)&r->top);
- GlobalToLocal((Point*)&r->bottom);
- }
-
-
-
- /*----------------------------------------------------------------------------
- LocalToGlobalRgn
-
- Convert a region from local to global coordinates.
-
- Entry: rgn = handle to region.
- ----------------------------------------------------------------------------*/
-
- void LocalToGlobalRgn (RgnHandle rgn)
- {
- Point where;
-
- SetPt(&where, 0, 0);
- LocalToGlobal(&where);
- OffsetRgn(rgn, where.h, where.v);
- }
-
-
-
- /*----------------------------------------------------------------------------
- GlobalToLocalRgn
-
- Convert a region from global to local coordinates.
-
- Entry: rgn = handle to region.
- ----------------------------------------------------------------------------*/
-
- void GlobalToLocalRgn (RgnHandle rgn)
- {
- Point where;
-
- SetPt(&where, 0, 0);
- LocalToGlobal(&where);
- OffsetRgn(rgn, -where.h, -where.v);
- }
-
-
-
- /*----------------------------------------------------------------------------
- GetFontNumber
-
- Get the font number corresponding to a font name (adapted from Think Ref).
-
- Entry: fontName = font name.
-
- Exit: function result = true if font exists.
- *fontNum = font number.
-
- If the font doesn't exist, the font number is set to Monaco, or to
- applFont if Monaco also doesn't exist.
- ----------------------------------------------------------------------------*/
-
- Boolean GetFontNumber (Str255 fontName, short *fontNum)
- {
- Str255 systemFontName;
-
- GetFNum(fontName, fontNum);
- if (*fontNum != 0) return true;
- GetFontName(0, systemFontName);
- if (EqualString(fontName, systemFontName, false, false)) return true;
- GetFNum("\pMonaco", fontNum);
- if (*fontNum == 0) *fontNum = applFont;
- return false;
- }
-
-
-
- /*----------------------------------------------------------------------------
- GetFontLineHeight
-
- Get the line height in pixels of a window font.
-
- Entry: wind = pointer to window.
-
- Exit: function result = line height.
- ----------------------------------------------------------------------------*/
-
- short GetFontLineHeight (WindowPtr wind)
- {
- FontInfo fInfo;
- GrafPtr port;
-
- GetPort(&port);
- SetPort(wind);
- GetFontInfo(&fInfo);
- SetPort(port);
- return fInfo.ascent + fInfo.descent + fInfo.leading;
- }
-
-
-
- /*----------------------------------------------------------------------------
- OutlineRegion
-
- Change a region into a tracing of its border which is appropriate
- for normal dragging.
-
- Entry: theRgn = handle to region.
-
- Exit: Region changed to outline of region.
-
- From Apple "HFS Drag Sample" sample code.
- ----------------------------------------------------------------------------*/
-
- void OutlineRegion (RgnHandle theRgn)
- {
- RgnHandle tempRgn;
-
- tempRgn = NewRgn();
- CopyRgn(theRgn, tempRgn);
- InsetRgn(tempRgn, 1, 1);
- DiffRgn(theRgn, tempRgn, theRgn);
- DisposeRgn(tempRgn);
- }
-